<template>
  <div class="realtimeBody">
    <div class="left">
      <div class="top">
        <el-input v-model="search.name" placeholder="请输入文字">
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <div class="video-left-top-box">
        <el-button  type="primary"  size="small"  @click="showDetailDialog('edit')">修改</el-button>
        <el-button  type="primary"  v-loading="btnLoading"  size="small"  @click="refreshEvent">刷新</el-button>
      </div>

      <div class="tongji">
        <div class="btn">
          <img
            src="../../../assets/images/real-time/inline.png"
            alt="inline"
          />
          <span class="txt">在线</span>
          <span class="num">{{ onlineCount }}</span>
        </div>
        <div class="btn">
          <img
            src="../../../assets/images/real-time/offline.png"
            alt="outline"
          />
          <span class="txt2">离线</span>
          <span class="num2">{{ offlineCount }}</span>
        </div>
				<div class="btn">
					<img
						src="../../../assets/images/real-time/outline.png"
						alt="outline"
					/>
					<span class="txt2">未知</span>
					<span class="num2">{{ outlineCount }}</span>
				</div>
         <div class="guanlibtn" @click="showDetailDialog('build')">新增</div>
      </div>
      <div class="realtime-tree WholeScroll">
        <el-tree
          :data="treeData"
          node-key="id"
          @node-click="handleNodeClick"
          default-expand-all
          :props="defaultProps"
          :expand-on-click-node="false"
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <div v-if="node.isLeaf == true" class="imgdiv">
              <img
								src="../../../assets/images/real-time/inline.png"
								alt=""
								v-if="data.onLine == 1"
							/>
              <img
								src="../../../assets/images/real-time/offline.png"
								alt=""
								v-if="data.onLine == 0"
							/>
							<img
								src="../../../assets/images/real-time/outline.png"
								alt=""
								v-if="data.onLine == 2"
							/>
            </div>
            <span class="label" :title="data.name">{{ data.name }}</span>
          </span>
        </el-tree>
      </div>
    </div>
    <div class="right" style="overflow: hidden">
      <iframe
        style="visibility: hidden"
        src="static/video/demo_embedded_for_iframe.html"
        id="iframe"
      ></iframe>
    </div>
    <FileDialog :postDetail="fileDialogData" />
    <Detail :postDetail="postDetailData" @reloadList="getTreeData(true)"></Detail>
    <!-- <Detail :postDetail="postDetailData" ></Detail> -->
  </div>
</template>

<script>
import {edit} from "@/api/buss/video";

window.addEventListener('message', function(e) {
  if (e && e.data) {
    switch (e.data.action) {
      case 'updateTitle': // 父页面将其标题发送过来，子页面保存该标题，以便创建插件窗口成功后将标题设置回给父页面
        that.initIframe()
        break
      case 'showInfo': // 父页面将其标题发送过来，子页面保存该标题，以便创建插件窗口成功后将标题设置回给父页面
        console.log('resize')
        that.fileDialogData = {
          show: true
        }
        break
      default:
        break
    }
  }
})
let iframe
let that
export default {
  methods: {
    showDetailDialog(flag, params={}) {
      console.log(flag, params)

      switch (flag) {
        case 'build':
          this.postDetailData = {
            show: true,
            type: 'build',
            params: params
            // searchProject: this.searchProject
          }
          break
        case 'edit':
          if(!this.detailId){
            this.$messageError('请选择设备')
            return;
          }
          params.detailId = this.detailId;
          this.postDetailData = {
            show: true,
            type: 'edit',
            params: params
          }
          break
      }
    },
    refreshEvent(){
      this.$httpPost('/buss/video/getTree', {})
        .then((res) => {
          if (res.code === 200) {
            this.treeData = res.data
            this.$set(this,'onlineCount',res.data[0].onLineCount)
            this.$set(this,'offlineCount',res.data[0].offLineCount)
            this.$set(this,'outlineCount',res.data[0].outLineCount)
            this.$messageSuccess('刷新完成！')
          }
        })
        .catch((res) => {
          this.$messageError(this.$route.name + '请求失败')
        })
    },
    getTreeData(){
      this.$httpPost('/buss/video/getTree', {})
        .then((res) => {
          if (res.code === 200) {
            this.treeData = res.data
            this.$set(this,'onlineCount',res.data[0].onLineCount)
            this.$set(this,'offlineCount',res.data[0].offLineCount)
            this.$set(this,'outlineCount',res.data[0].outLineCount)
          }
        })
        .catch((res) => {
          this.$messageError(this.$route.name + '请求失败')
        })
    },
    handleNodeClick(data) {
      console.log(data)
      if (!data && this.rightAmount === '1') {
        return
      }
      if(data.isLeaf){
        this.detailId = data.id
        // this.showVideo(data.indexCode)
      }
    },
    // 切换视图数量
    changeShowView(type, item) {
      if (!item && this.rightAmount === type) {
        return
      }
      this.showVideo(item.streamAddress)
    },
    initIframe() {
      let params =
        '{\n' +
        '        "argument": {\n' +
        '          "appkey": "25146971",\n' +
        '          "ip": "36.133.41.13",\n' +
        '          "port": 10443,\n' +
        '          "secret": "S3mp7HhH42gZiaCF7p3w",\n' +
        '          "enableHTTPS": 1,\n' +
        '          "layout": "2x2",\n' +
        '          "playMode": 0\n' +
        '        },\n' +
        '        "funcName": "init"\n' +
        '      }'
      iframe.contentWindow.requestInterface(params.replace(/(\s*)/g, ''))
      console.log(iframe.contentWindow)
      console.dir(iframe)
      this.replaceIframe()
    },
    showVideo(item) {
      console.log(item)
      let params = `{
    "argument": {
        "cameraIndexCode": "${item}",
        "ezvizDirect": 0,
        "gpuMode": 0,
        "streamMode": 0,
        "transMode": 1,
        "wndId": -1
    },
    "funcName": "startPreview"
}

`
      iframe.contentWindow.requestInterface(params.replace(/(\s*)/g, ''))
    },
    replaceIframe() {
      let width = iframe.clientWidth
      let height = iframe.clientHeight
      let leftPos = iframe.offsetLeft + 225
      let topPos = iframe.offsetTop + 60
      iframe.contentWindow.postMessage({
        action: 'updateInitParam',
        iframeClientPos: { left: leftPos, top: topPos },
        showSize: { width: width, height: height }
      })
    },
  },
  name: 'real-time-preview',
  data() {
    return {
      btnLoading:false,
      postDetailData: {
        show: false,
        type: 'build'
      },
      rightAmount: 2, // 右侧显示的数值的平方
      search: {
        name: ''
      },
      detailId : '',
      treeData: [],
      searchKey: '',
      searchSize: 200,
      fileDialogData: {
        show: false
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
			onlineCount: 0,
			offlineCount: 0,
			outlineCount: 0,
    }
  },
  components: {
    FileDialog: require('./detail/fileDialog').default,
    Detail: require('./detail/videoView-setting').default,
  },
  mounted() {
    //获取iframe
    that = this
    setTimeout(() => {
      iframe = $('#iframe')[0]
      iframe.contentWindow.width = iframe.clientWidth
      iframe.contentWindow.height = iframe.clientHeight
      iframe.contentWindow.initPlugin()
    }, 2000)
		this.getTreeData()
  }
}
</script>

<style lang="scss" scoped>
.realtimeBody {
  width: 100%;
  height: 90vh;
  background: #ffffff;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  display: flex;
  .left {
    width: 240px;
    height: 100%;
    background: #f8f9fb;
    border-radius: 4px 0px 0px 4px;
    .top {
      padding: 16px;
      border-bottom: 1px solid rgba(220, 224, 227, 1);
    }
    .tongji {
      padding: 16px;
      display: flex;
      align-items: center;
      border-bottom: 1px solid rgba(220, 224, 227, 1);
      .btn {
        display: flex;
        align-items: center;
        .txt {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #46c180;
          margin-right: 6px;
        }
        .num {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #46c180;
        }
        .txt2 {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #b4b7be;
          margin-right: 6px;
        }
        .num2 {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #b4b7be;
        }
      }
      .guanlibtn {
        width: 36px;
        height: 22px;
        background: #46c180;
        border-radius: 3px;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #ffffff;
        text-align: center;
        margin-left: 18px;
        cursor: pointer;
      }
    }
    .realtime-tree {
      // height: calc(100%-200px);
      max-height: 81.58vh;
      overflow-y: auto;
      padding: 16px;
      background-color: #ffffff;
      overflow-x: scroll;
      .el-tree {
        width: max-content;
      }
      .custom-tree-node {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        .label {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #4e5157;
          margin-right: 10px;
        }
      }
    }
  }
  .right {
    overflow-y: hidden;
    padding: 20px;
    flex: 1;

    .change {
      height: 28px;
      margin: 16px 22px;

      .change_1,
      .change_2,
      .change_3 {
        width: 34px;
        height: 28px;
        float: right;
        margin-left: 18px;
        cursor: pointer;
      }

      .change_one {
        float: right;
      }

      .change_1 .change_one {
        width: 34px;
        height: 28px;
        border-radius: 5px;
      }

      .change_2 .change_one {
        width: 15px;
        height: 13px;
        border-radius: 3px;
        margin: 0 2px 2px 0;
      }

      .change_3 .change_one {
        width: 9px;
        height: 8px;
        border-radius: 2px;
        margin: 0 2px 2px 0;
      }
    }

    .content {
      padding: 0 25px;
      flex-direction: column;

      .one {
        float: left;
        background-color: #fff;
        position: relative;
        box-sizing: border-box;
        border: 4px solid #2c323e;

        .content {
          position: absolute;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          padding: 0;
        }

        .message_wrap {
          position: absolute;
          top: 0;
          left: 0;
          height: 40px;
          width: 100%;
          line-height: 40px;
          z-index: 50;
          background: linear-gradient(
            to right,
            rgba(0, 0, 0, 0.7),
            rgba(0, 0, 0, 0)
          );

          .message_left {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;

            .name {
              font-size: 16px;
              color: #fff;
              padding-left: 16px;
            }
          }
        }

        &:hover {
          border: 4px solid #4f8edd;
        }
      }

      .between {
        width: 25px;
      }

      .between_column {
        height: 25px;
      }

      .one_1 {
        padding-bottom: 52%;
      }

      .one_2 {
        padding-bottom: 24%;
      }

      .one_3 {
        padding-bottom: 16%;
      }
    }
  }

  #iframe {
    width: 100%;
    height: 100%;
  }
}
.video-left-top-box{
  width: 90%;
  margin: 0  auto;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
  padding-top: 15px;
}
</style>
